<?php

function debug($variable) {
	echo '<pre>';
	echo print_r($variable);
	echo '</pre>';
}

//Random string
function random_string($type = 'alnum', $len = 8) {
	switch($type) {
		case 'basic'	: 
			return mt_rand();
			break;
		case 'alnum'	:
		case 'numeric'	:
		case 'nozero'	:
		case 'alpha'	:
			switch ($type) {
				case 'alpha' :	
					$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
					break;
				case 'alnum' :	
					$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
					break;
				case 'numeric' :	
					$pool = '0123456789';
					break;
				case 'nozero' :	
					$pool = '123456789';
					break;
			}

			$str = '';
			for ($i=0; $i < $len; $i++) {
				$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
			}
			return $str;
			break;
		case 'unique'	:
		case 'md5'		:
			return md5(uniqid(mt_rand()));
			break;
		break;
	}
}

//get contents with curl
function get_content($url) {

	$ch = curl_init();
	
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

	$string = curl_exec($ch);
	curl_close($ch);
	return $string;
}

//Get image
function getImage($url, $baseUrl){
	$html_content_string = file_get_contents($url);
	$xpath_string = '/html/body/form/table/tbody/tr[10]/td[1]/img';

	$img = preg_match('/(?<=\<img\ssrc\=\").*(?=">)/', $html_content_string, $m);
	if ($m){
		return $baseUrl.$m[0];
	}
	return false;
}

function submit_data($url, $data) {
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

	$result = curl_exec($ch);
	$info = curl_getinfo($ch);
	curl_close($ch);
	echo $result;
}


function curl_cookie($url) {
	/* STEP 1. let’s create a cookie file */
	$ckfile = tempnam("/tmp", "CURLCOOKIE");
	
	/* STEP 2. visit the homepage to set the cookie properly */
	$ch = curl_init($url);
	curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec ($ch);
	
	/* STEP 3. visit cookiepage.php */
	$ch = curl_init ($url);
	curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec ($ch);
}

//Inint variable
$url = "http://marketeronline.site11.com/index.php?a=signup";
$baseUrl = "http://marketeronline.site11.com/index.php";


$password = random_string();
$email = random_string().'@'.random_string('alpha',6).'.com';
$question = random_string();

$data = array(
	'fullname' => random_string(),
	'username' => random_string(),
	'password' => $password,
	'password2' => $password,
	'email' => $email,
	'email1' => $email,
	'sq' => $question,
	'sa' => $question,
	'agree' => 1,
);

//get the page
$results = get_content($url);

curl_cookie($url);

if (!empty($_POST)) {
	$data['validation_number'] = $_POST['validation_number'];
	
	$dataString = '';
	foreach($data as $key=>$value) {
		$dataString .= $key.'='.$value.'&';
	}
	rtrim($dataString,'&');
 	submit_data($url, $dataString);
 	debug($data);
 	
 	if (preg_match_all('/<img[^>][^>]*src\=\"([^\"]*)\"[^>]*>/s', $results , $captchaimages)) {
 		$captcha = $captchaimages[1][0]; //this returns image url with amp;
 		$captcha = str_replace("amp;","",$captcha); //remove amp; from the string
 		echo '<img src='.$baseUrl.$captcha.'>';
 	}
}

?>
<?php //if (preg_match_all('/<img[^>][^>]*src\=\"([^\"]*)\"[^>]*>/s', $results , $captchaimages)): ?>
	<?php 
// 		$captcha = $captchaimages[1][0]; //this returns image url with amp;
// 		$captcha = str_replace("amp;","",$captcha); //remove amp; from the string
		
		//vouaaalaaaa now its working like charm
// 		$file = get_content($baseUrl.$captcha);
// 		file_put_contents("captcha.jpg", "$file");
	?>
<?php //else:?>
	<?php //echo $results?>
<?php //endif?>

<form method="post">
	<input type="text" size="15" class="inpts" name="validation_number">
	<button type="submit">Submit</button>
</form>
<iframe style="position: absolute; top: 30px; right: 30px" src="<?php echo $url;?>" width="500" height="500"></iframe>


